装载问题的分支限界算法实现

今日研究分支限界算法,拿了王晓东老师的算法设计与分析教材。仔细读懂了书中算法和代码且找出了算法的些许错误,在我写的第一篇原创博客中和大家分享。

对于装载问题这个实例,老师首先找到了O(2^n)复杂度的算法找到最大装载问题的解,然后对这个算法进行类似剪枝的操作,使得算法更加优化。为了找到最优解对应的装载策略,对代码进行了改进,在搜索子集树中保存当前已构造出的子集树中的路径指针,从而可在结束搜索后向根结点回溯,从而构造出最优装载。


直接上代码:

#include"queue.h"


template<class Type>
class Qnode
{
//friend void enqueue(Queue<Qnode<Type>*>&q, Qnode<Type>*&qn,Qnode<Type>*&best,Type result,Type bestw,int i,int n,bool ch,int bestx[]);
//friend Type maxloading(Type a[],Type c, int n, int bestx[]);
public:
Qnode<Type>* parent;
bool chosen;
Type weigth;
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include #include using namespace std; ifstream infile; ofstream outfile; class Node { friend int func(int*, int, int, int*); public: int ID; double weight;//物品的重量 }; bool comp1(Node a, Node b) //定义比较规则 { return a.weight > b.weight; } class Load; class bbnode; class Current { friend Load; friend struct Comp2; private: int upweight;//重量上界 int weight;//结点相应的重量 int level;//活结点在子集树中所处的层次 bbnode* ptr;//指向活结点在子集树中相应结点的指针 }; struct Comp2 { bool operator () (Current *x, Current *y) { return x->upweightupweight; } }; class Load { friend int func(int*, int, int, int*); public: int Max0(); private: priority_queue<Current*, vector, Comp2>H;//利用优先队列(最大堆)储存 int limit(int i); void AddLiveNode(int up, int cw, bool ch, int level); bbnode *P;//指向扩展结点的指针 int c;//背包的容量 int n;//物品的数目 int *w;//重量数组 int cw;//当前装载量 int *bestx;//最优解方案数组 }; class bbnode { friend Load; friend int func( int*, int, int, int*); bbnode* parent; bool lchild; }; //结点中有双亲指针以及左儿子标志 int Load::limit(int i) //计算结点所相应重量的上界 { int left,a; left= c - cw;//剩余容量 a = cw; //b是重量上界,初始值为已经得到的重量 while (i <= n && w[i] parent = P; b->lchild = ch; Current* N = new Current; N->upweight = up; N->weight = cw; N->level = level; N->ptr = b; H.push(N); } int Load::Max0() { int i = 1; P = 0; cw = 0; int bestw = 0; int up = limit(1); while (i != n + 1) { int wt = cw + w[i]; //检查当前扩展结点的左儿子结点 if (wt bestw) bestw =wt; AddLiveNode(up,wt, true, i + 1); } up = limit(i + 1); //检查当前扩展结点的右儿子结点 if (up >= bestw)//如果右儿子可行 { AddLiveNode(up,cw, false, i + 1); } Current* N = H.top(); //取队头元素 H.pop(); P = N->ptr; cw = N->weight; up = N->upweight; i = N->level; } bestx = new int[n + 1]; for (int j = n; j > 0; --j) { bestx[j] = P->lchild; P = P->parent; } return cw; } int func(int *w, int c, int n, int *bestx) //调用Max0函数对子集树的优先队列式进行分支限界搜索 { int W = 0; //初始化装载的总质量为0 Node* Q = new Node[n]; for (int i = 0; i < n; ++i) { Q[i].ID = i + 1; Q[i].weight = w[i+1]; W += w[i+1]; } if (W <= c)//如果足够装,全部装入 return W; sort(Q, Q + n, comp1); //首先,将各物品按照重量从大到小进行排序; Load K; K.w = new int[n + 1]; for (int j = 0; j < n; j++) K.w[j + 1] = w[Q[j].ID]; K.cw = 0; K.c = c; K.n = n; int bestp = K.Max0(); for (int k = 0; k < n; k++) { bestx[Q[k].ID] = K.bestx[k + 1]; } delete []Q; delete []K.w; delete []K.bestx; return bestp; } int main() { int*w,*Final; int c,n,i,best; infile.open("input.txt",ios::in); if(!infile) { cerr<<"open error"<>c; infile>>n; w=new int[n+1]; for(i=1;i>w[i]; infile.close(); Final = new int[n+1]; best = func( w, c, n, Final); outfile.open("output.txt",ios::out); if(!outfile) { cerr<<"open error"<<endl; exit(1); } outfile << best << endl; for (int i = 1; i <= n; ++i) { outfile<<Final[i]<<" "; } outfile.close(); return 0; }
装载问题是一类优化问题,其目标是在给定的一些物品中选择若干个物品,使得其总重量不超过限制且总价值最大化。下面是使用分支限界法求解装载问题的 Python 代码: ``` class Item: def __init__(self, weight, value): self.weight = weight self.value = value def load(capacity, items): items.sort(key=lambda x: x.value / x.weight, reverse=True) # 按单位价值排序 n = len(items) best_value = 0 # 当前最优解 best_items = [] # 当前最优解对应的物品列表 def bound(node): if node.weight > capacity: return 0 # 超出容量,不可行 value_bound = node.value weight_bound = node.weight j = node.level + 1 while j < n and weight_bound + items[j].weight <= capacity: value_bound += items[j].value weight_bound += items[j].weight j += 1 if j < n: value_bound += (capacity - weight_bound) * items[j].value / items[j].weight return value_bound def dfs(node): nonlocal best_value, best_items if node.level == n - 1: if node.value > best_value: best_value = node.value best_items = [items[i] for i in node.path] return if node.value + bound(node) <= best_value: return # 剪枝 # 不选当前物品 dfs(Node(node.level + 1, node.weight, node.value, node.path)) # 选当前物品 if node.weight + items[node.level + 1].weight <= capacity: dfs(Node(node.level + 1, node.weight + items[node.level + 1].weight, node.value + items[node.level + 1].value, node.path + [node.level + 1])) class Node: def __init__(self, level, weight, value, path): self.level = level self.weight = weight self.value = value self.path = path dfs(Node(-1, 0, 0, [])) return best_value, best_items ``` 代码中,`Item` 类表示一个物品,包含重量和价值两个属性。`load` 函数接受一个容量和一个物品列表作为输入,返回最优解的总价值和所选物品列表。函数中使用了一个 `Node` 类表示搜索树中的节点,包含当前节点的层数、当前已选物品的总重量和总价值、以及已选物品的编号列表。`bound` 函数计算当前节点的上界,即将剩余物品按单位价值从高到低排序后依次加入,直到超出容量为止,然后将剩余部分按单位价值填满。`dfs` 函数是搜索过程的核心,其首先判断当前节点是否是叶子节点,如果是,则更新最优解。否则,先进行一次剪枝,如果当前节点的上界小于等于当前最优解,则直接返回;否则,继续搜索两个子节点,分别对应选或不选当前物品。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值